package xuan.mall.system.interfaces.dto.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import xuan.mall.common.enums.ResultCode;

import java.util.List;

public class PageResponse<T> extends Response<PageResponse.PageData<T>> {
    
    // 分页响应构造
    public PageResponse(ResultCode code,
                        List<T> content,
                        int page,
                        int size,
                        long totalElements) {
        super(code, new PageData<>(content, page, size, totalElements));
    }

    // 分页数据封装
    @Data
    @AllArgsConstructor
    public static class PageData<R> {
        private List<R> content;          // 当前页数据
        private int page;                 // 当前页码
        private int size;                 // 每页大小
        private long totalElements;       // 总记录数
        private int totalPages;           // 总页数（自动计算）
        
        public PageData(List<R> content, int page, int size, long totalElements) {
            this.content = content;
            this.page = page;
            this.size = size;
            this.totalElements = totalElements;
            this.totalPages = (int) Math.ceil((double) totalElements / size);
        }
    }

    // 分页成功响应
    public static <T> PageResponse<T> success(List<T> content, 
                                             int page, 
                                             int size, 
                                             long total) {
        return new PageResponse<>(ResultCode.SUCCESS, content, page, size, total);
    }
}